package com.testingedu.action.background;

import java.util.ArrayList;
import java.util.List;

import com.testingedu.jdbcDao.TOperateDao;
import com.testingedu.jdbcDao.TOperateInRoleDao;
import com.testingedu.jdbcDao.TRoleDao;
import com.testingedu.model.TOperate;
import com.testingedu.model.TRole;

/**
 * 
 * @author lsww
 * @E-mail lsww90@Gmail.com
 * @createtime 2012-11-21 下午08:54:56
 * @version 1.0
 * @since JDK 1.6
 */
public class TOperateInRoleAction extends RequestContext {

	private static final long serialVersionUID = -9146777136466760390L;
	private static TOperateInRoleDao tOperateInRoleDao = new TOperateInRoleDao();
	private static TOperateDao tOperateDao = new TOperateDao();

	public void _list() throws Exception {
		TOperate tOperate = new TOperate();
		PrepareParameter.execute(request, tOperate);
		String str_roleId = request.getParameter("roleId");
		if (str_roleId == null || str_roleId.trim().length() == 0) {
			str_roleId = "0";
		}
		createQueryResult();
		tOperateDao.list(queryResult, null, Integer.parseInt(str_roleId),
				tOperate);
		TRole tRole = new TRoleDao().info(Integer.parseInt(str_roleId));
		request.setAttribute("queryResult", queryResult);
		request.setAttribute("tRole", tRole);
		request.getRequestDispatcher("/WEB-INF/admin/jsp/oir/list.jsp")
				.forward(request, response);
	}

	public void _add() throws Exception {
		String url = request.getParameter("url");
		if ("jsp".equals(url)) {
			myOperate();
			request.getRequestDispatcher("/WEB-INF/admin/jsp/oir/add.jsp")
					.forward(request, response);
		} else {
			List<Integer> operateIds = new ArrayList<Integer>();
			String[] values = request.getParameter("value").split("o");
			for (int i = 0; i < values.length; i++) {
				operateIds.add(Integer.parseInt(values[i]));
			}
			Integer roleId = Integer.parseInt(request.getParameter("roleId"));
			tOperateInRoleDao.add(roleId, operateIds);
		}
	}

	private void myOperate() throws Exception {
		TOperate tOperate = new TOperate();
		TOperateDao tOperateDao = new TOperateDao();
		PrepareParameter.execute(request, tOperate);
		Integer roleId = Integer.parseInt(request.getParameter("roleId"));
		Integer userId = getTUserId();
		createQueryResult();
		tOperateDao._myOperate(queryResult, userId, roleId, tOperate);
		TRole tRole = new TRole();
		tRole = new TRoleDao().info(roleId);
		request.setAttribute("tRole", tRole);
		request.setAttribute("queryResult", queryResult);
	}

	public void _delete() throws Exception {
		String url = request.getParameter("url");
		if ("jsp".equals(url)) {
			roleOperate();
			request.getRequestDispatcher("/WEB-INF/admin/jsp/oir/delete.jsp")
					.forward(request, response);
		} else {
			List<Integer> operateIds = new ArrayList<Integer>();
			String[] values = request.getParameter("value").split("o");
			for (int i = 0; i < values.length; i++) {
				operateIds.add(new Integer(Integer.parseInt(values[i])));
			}
			Integer roleId = Integer.parseInt(request.getParameter("roleId"));
			tOperateInRoleDao.delete(roleId, operateIds);
		}
	}

	private void roleOperate() throws Exception {
		TOperate tOperate = new TOperate();
		TOperateDao tOperateDao = new TOperateDao();
		PrepareParameter.execute(request, tOperate);
		Integer roleId = Integer.parseInt(request.getParameter("roleId"));
		createQueryResult();
		tOperateDao.list(queryResult, null, roleId, tOperate);
		TRole tRole = new TRole();
		tRole = new TRoleDao().info(roleId);
		request.setAttribute("tRole", tRole);
		request.setAttribute("queryResult", queryResult);
	}
}
